﻿#include "uitableitemdelegate.h"
#include <QAbstractItemView>
#include <QPainter>
#include <QTableWidget>

UiTableItemDelegate::UiTableItemDelegate(
        QAbstractItemView *table,
        QObject *parent)
    : QStyledItemDelegate(parent), m_table(table)
{
}

void UiTableItemDelegate::paint(
        QPainter *painter,
        const QStyleOptionViewItem &option,
        const QModelIndex &index) const
{
    // 效果: 使鼠标悬停在一个项(item)时, 高亮一整行

    if (option.state.testFlag(QStyle::State_MouseOver) &&
        index.isValid()) { // 检查是否有鼠标悬停在某个有效的 item

        QAbstractItemModel *m = m_table->model();
        int row = index.row();
        int columnCount = m->columnCount();
        for (int i = 0; i < columnCount; i++) {
            QModelIndex idx = m->index(row, i);
            QStyleOptionViewItem op = option;
            op.index = idx;
            op.rect = m_table->visualRect(idx);
            QStyledItemDelegate::paint(painter, op, idx);
        }
        return;
    }

    return QStyledItemDelegate::paint(painter, option, index);
}
